<?php

/**
 * 状态码说明
 * 900 全局  900+（1zhanglitong,2wechat,3alipay支付方式）+（1-9对应模块）+（1-9具体状态）
 * 例如：900101对应余额支付外卖模块错误1
 * User: cth
 * Date: 2017/6/13
 * Time: 上午10:13
 */
namespace App\Repositories;

//门面
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
//模型
use App\Models\Shop;
use App\Models\UserCheck;
use App\Models\WalletLog;
use App\Models\Order;
use App\Models\WeixinUser;
use App\Models\OrderShare;
//工具
use Yansongda\Pay\Pay;
use App\Tools\CouldPrint;
use App\Tools\WechatSend;
use App\Tools\SqlIncrement;
use App\Tools\Jpush;
use App\Tools\MoneyChange;

class PayRepository
{


    protected $pay_way;  //支付方式，zhanglitong,wechat,alipay
    protected $order_id; //支付订单id
    protected $pay_port; //支付端口，web,pc,miniapp,wechatapp,app（掌里通支付默认传web）
    protected $config;   //微信，支付宝配置
    protected $order;    //支付的订单
    protected $payword;  //支付的密码（仅掌里通支付需要）
    protected $openid;   //用户微信openid
    protected $token;
    /**
     * 统一支付接口
     */
    public function payOrder($pay_way,$order_id,$pay_port,$payword='',$openid='',$token=false)
    {
        //验证参数
        if(!$pay_way||!$order_id||!$pay_port)
        {
            return show("900","缺少参数");
        }
        //全局参数
        $this->pay_way  = $pay_way;
        $this->order_id = $order_id;
        $this->pay_port = $pay_port;
        $this->payword  = $payword;
        $this->openid   = $openid;
        $this->config   = config("pay");
        $this->token    = $token;

        //验证订单
        $order = Order::find($this->order_id);
        if(!$order){
            return show("900","订单不存在");
        }else{
            $this->order = $order;
        }
        //调用支付方式
        switch($this->pay_way){
            case 'zhanglitong':
                if(!$this->token){//auth验证
                    if(!Auth::id())
                    {
                        return show("90010","没有登录！");
                    }
                    $user = UserCheck::find(Auth::id());
                }else{//token验证
                    $WeixinUser = WeixinUser::where('token',$this->token)->first();
                    if(!$WeixinUser){
                        return show("90010","无效token！");
                    }else{
                        $user= UserCheck::find($WeixinUser->user_id);
                        if(!$user){
                            return show("90010","用户不存在！");
                        }
                    }
                }

                if($user->payword===null)
                {
                    return show("90011","请设置支付密码！");
                }
                if(!Hash::check($this->payword,$user['payword']))
                {
                    return show("90012","支付密码不正确！");
                }
                /*if($user->id != $this->order->user_id)
                {
                    return show("9003", "无权支付此订单！");
                }*/
                return $this->zhanglitong($user);
                break;
            case 'wechat':
                return $this->wechat();
                break;
            case 'alipay':
                return $this->alipay();
                break;
            default://错误
                return show("900","请选择正确的支付方式");
                break;
        }
    }
//    /**
//     * 余额变动
//     * $id=用户id,$money=变动金,$type=类型（o减 1加）,$sign=交易备注,$trace_way交易方式（0余额1微信2支付宝3其他）
//     */
//    protected function  add_money($id,$money,$type=0,$sign='',$trace_way){
//        //检查用户
//        $changeUser = UserCheck::find($id);
//        if(!$changeUser){
//            return false;
//        }else{
//            if($type==0){//减钱
//                $changeUser->money = $changeUser->money - $money;
//                $now_money = $changeUser->money;
//                $changeUser->save();
//            }else if($type==1){//加钱
//                $changeUser->money = $changeUser->money + $money;
//                $now_money = $changeUser->money;
//                $changeUser->save();
//            }else{
//                return false;
//            }
//            $this->add_wallet_log($id,$money,$now_money,$type,$sign,$trace_way);
//            return true;
//        }
//    }
//    /**
//     * 添加流水
//     */
//    protected function  add_wallet_log($id,$money,$now_money,$type,$sign='',$trace_way=0){
//        WalletLog::create([
//            'user_id'=>$id,
//            'serial_number'=>randID(),
//            'type'=>$type,
//            'work_capital'=>$money,
//            'now_money'=>$now_money,
//            'sign' =>$sign,
//            'trace_way'=>$trace_way,
//        ]);
//        return true;
//    }
    /**
     * 掌里通支付
     */
    protected  function zhanglitong($user){
        switch ($this->order->order_type)
        {
            case 0://外卖
                return $this->zhanglitongtakeout($user);
                break;
            case 1://预定
                return $this->zhanglitongreserve($user);
                break;
            case 2://排队
                return $this->zhanglitongqueue($user);
                break;
            case 3://扫码
                return $this->zhanglitongscan($user);
                break;
            default://错误
                return show("90001","不支持该类型订单付款");
                break;
        }

    }
    /**
     * 掌里通支付外卖模块
     */
    protected function zhanglitongtakeout($user){
        if($this->order->order_status!=1){
            return show("900010","该订单状态不可支付！");
        }
        if(($user->money) < ($this->order->order_amount)){
            return show("900011","余额不足！");
        }else{
            //减钱加流水
            $money = new MoneyChange();
            $money->add_money($user->id,$this->order->order_amount,0,'(外卖)订单编号：'.$this->order->order_number,0,'外卖');
            //更新订单
            $orderupdate = Order::find($this->order->id);
            $orderupdate->order_status = 2;
            $orderupdate->pay_way = 0;
            $orderupdate->save();
            //打印订单
            $printer = new CouldPrint();
            $printer->printer($this->order->id);
            //推送消息
            $wxsend = new WechatSend();
            $wxsend->orderTemplate($this->order->id);
            //app语音
            $appsend = new Jpush();
            $appsend->sendOrder($this->order->id);
            return show("200","支付成功",["order_id"=>$this->order->id]);
        }
    }
    /**
     * 掌里通支付预定模块
     */
    protected function zhanglitongreserve($user){
        if($this->order->order_status==1){//第一次付款
            if(($user->money) < ($this->order->order_amount)){
                return show("900021","余额不足！");
            }
            //$shop = Shop::find($this->order->shop_id);
            //加钱加流水
            //$this->add_money($shop->user_id,$this->order->order_amount,1,'(预定)订单编号：'.$this->order->order_number,0);
            $money = new MoneyChange();
            $money->add_money($user->id,$this->order->order_amount,0,'(预定)订单编号：'.$this->order->order_number,0,'预定');
            //更新订单
            $orderupdate = Order::find($this->order->id);
            $orderupdate->order_status = 2;
            $orderupdate->pay_way=0;
            $orderupdate->save();
            //打印订单
            $printer = new CouldPrint();
            $printer->printer($this->order->id);
            //推送消息
            $wxsend =  new WechatSend();
            $wxsend->orderTemplate($this->order->id);
            //app语音
            $appsend = new Jpush();
            $appsend->sendOrder($this->order->id);
            return show("200","支付成功",["order_id"=>$this->order->id]);
        }else if($this->order->order_status==3){//第二次付款
            if(($user->money) < ($this->order->order_price-$this->order->order_amount)){
                return show("900021","余额不足！");
            }
            $shop = Shop::find($this->order->shop_id);
            //加钱加流水
            $money = new MoneyChange();
            $money->add_money($shop->user_id,$this->order->order_price,1,'(结账)订单编号：'.$this->order->order_number,0,'预定',$this->order->id);
            $money->add_money($user->id,$this->order->order_price-$this->order->order_amount,0,'(结账)订单编号：'.$this->order->order_number,0,'预定');
            //更新订单
            $orderupdate = Order::find($this->order->id);
            $orderupdate->order_status = 4;
            $orderupdate->save();
            //打印订单
            $printer=new CouldPrint();
            $printer->printer($this->order->id);
            //推送消息
            $wxsend =new WechatSend();
            $wxsend->orderTemplate($this->order->id);
            //数据库自增
            $sqladd =new SqlIncrement();
            $sqladd->dishIncrement($this->order->id);
            //app语音
            $appsend = new Jpush();
            $appsend->sendOrder($this->order->id);
            return show("200","支付成功",["order_id"=>$this->order->id]);
            }else{
                return show("900020","该订单状态不可支付！");
            }

    }
    /**
     * 掌里通支付排队模块
     */
    protected function zhanglitongqueue($user){
        if($this->order->order_status!=1){
            return show("900030","该订单状态不可支付！");
        }
        if(($user->money) < ($this->order->order_amount)){
            return show("900031","余额不足！");
        }else{
            //加钱加流水
            $money = new MoneyChange();
            $money->add_money($user->id,$this->order->order_amount,0,'(排队)订单编号：'.$this->order->order_number,0,'排队');
            //更新订单
            $orderupdate = Order::find($this->order->id);
            $orderupdate->order_status = 4;
            $orderupdate->pay_way = 0;
            $orderupdate->save();
            //打印订单
            $printer = new CouldPrint();
            $printer->printer($this->order->id);
            //推送消息
            $wxsend = new WechatSend();
            $wxsend->orderTemplate($this->order->id);
            //打印模板
            //app语音
            $appsend = new Jpush();
            $appsend->sendOrder($this->order->id);
            return show("200","支付成功",["order_id"=>$this->order->id]);
        }
    }
    /**
     * 掌里通支付扫码模块
     */
    protected function zhanglitongscan($user){
        if($this->order->order_status!=1){
            return show("900040","该订单状态不可支付！");
        }
        if(($user->money) < ($this->order->order_amount)){
            return show("900041","余额不足！");
        }else{
            $shop = Shop::find($this->order->shop_id);
            //加钱加流水
            $money = new MoneyChange();
            $money->add_money($shop->user_id,$this->order->order_price,1,'(扫码)订单编号：'.$this->order->order_number,0,'扫码');
            $money->add_money($user->id,$this->order->order_amount,0,'(扫码)订单编号：'.$this->order->order_number,0,'扫码');
            //更新订单
            $orderupdate = Order::find($this->order->id);
            $orderupdate->order_status = 4;
            $orderupdate->pay_way = 0;
            $orderupdate->save();
            //打印订单
            $printer= new CouldPrint();
            $printer->printer($this->order->id);
            //推送消息
            $wxsend = new WechatSend();
            $wxsend->orderTemplate($this->order->id);
            //数据库自增
            $sqladd = new SqlIncrement();
            $sqladd->dishIncrement($this->order->id);
            //app语音
            $appsend = new Jpush();
            $appsend->sendOrder($this->order->id);
            return show("200","支付成功",["order_id"=>$this->order->id]);
        }
    }
    /**
     * 微信支付
     */
    protected  function wechat(){
        $order =  $this->get_wechat_order();
        switch ($this->pay_port)
        {

            case 'web'://手机网页
                $order['out_trade_no'] ='web-'.$order['out_trade_no'];
                //$order['total_fee']=1;
                $order['spbill_create_ip'] = get_ip();
                return Pay::wechat($this->config['wechat'])->wap($order);
                break;
            case 'pc'://电脑网页
                //处理一个订单号只能生成一张二维码的问题
                $order['out_trade_no'] =rand(1000,9999).'-'.$order['out_trade_no'];
                $order['spbill_create_ip'] = get_ip();
                $order['product_id'] = time();
                //得到用户openID暂时写死
                //$order['openid'] = $this->openid;
                //$order['openid'] = 'oRZv9tk0dsAYLAsXRGncfu1naH-0';
                $result = Pay::wechat($this->config['wechat'])->scan($order)->code_url;
                return ["code"=>200,"message"=>"获取支付参数成功","data"=>$result,"port_type"=>"pc"];
                break;
            case 'wechatapp'://微信公众号
                $order['out_trade_no'] ='wechatapp-'.$order['out_trade_no'];
                //得到用户openID暂时写死
                $order['openid'] = $this->openid;
                //return $order;
                $result = Pay::wechat($this->config['wechat'])->mp($order);
                return ["code"=>200,"message"=>"获取支付参数成功","data"=>$result,"port_type"=>"wechatapp"];
                break;
            case 'miniapp'://微信小程序
                $order['out_trade_no'] ='miniapp-'.$order['out_trade_no'];
                //得到用户openID暂时写死
                $order['spbill_create_ip'] = get_ip();
                $order['openid'] = $this->openid;
                $result = Pay::wechat($this->config['wechat'])->miniapp($order);
                try{
                    $prepay_id_str = $result->package;
                    $prepay_id = substr($prepay_id_str,10);
                    DB::table('orders')->where('id',$this->order_id)->update(['prepay_id'=>$prepay_id]);
                }catch (\Exception $e){
                    return['code'=>"200","message"=>"微信小程序支付参数返回成功","data" =>$result];
                }
                return['code'=>"200","message"=>"微信小程序支付参数返回成功","data" =>$result];
                break;
            case 'app'://平台app(暂时不用，等APP绑定了微信支付先)
                $order['out_trade_no'] ='app-'.$order['out_trade_no'];
                $result = Pay::wechat($this->config['wechat'])->app($order);
                break;
            default://错误
                return show("90011","不支持该支付类型付款!");
                break;
        }

    }

    /**
     * 微信支付订单
     */
    protected function get_wechat_order(){
        switch ($this->order->order_type)
        {
            case 0://外卖
            case 2://排队
            case 3://扫码
            case 4://直接付款
            case 5://充值
                if($this->order->order_status!=1){
                    return show("90010","该订单状态不可支付！");
                }
                //订单价格处理
                return [
                    'out_trade_no' => $this->order->order_number.'_'.$this->order->id,
                    'body' => '掌里通订单支付',
                    'total_fee' => 100*$this->order->order_amount,
                ];
                break;
            case 6://优惠卡
                if($this->order->order_status!=1){
                    return show("90010","该订单状态不可支付！");
                }
                $money = false;
                $sharecard = config('Text')['shareCards'];
                foreach($sharecard as $v){
                    if($v['money']==$this->order->order_amount){
                        $money = true;
                    }
                }
                if(!$money){
                    return show(90010,'该订单不可支付');
                }
                //订单价格处理
                return [
                    'out_trade_no' => $this->order->order_number.'_'.$this->order->id,
                    'body' => '掌里通订单支付',
                    'total_fee' => 100*$this->order->order_amount,
                ];
                break;
            case 1://预定
                if($this->order->order_status==1){
                    //订单价格处理
                    return [
                        'out_trade_no' => $this->order->order_number.'-1'.'_'.$this->order->id,
                        'body' => '掌里通订单支付',
                        'total_fee' => 100*$this->order->order_amount,
                    ];
                }else if($this->order->order_status==3){
                    //订单价格处理
                    return [
                        'out_trade_no' => $this->order->order_number.'-2'.'_'.$this->order->id,
                        'body' => '掌里通订单支付',
                        'total_fee' => 100*($this->order->order_price-$this->order->order_amount),
                    ];
                }else{
                     return show("90010","该订单状态不可支付！");
                }
                break;
            default://错误
                return show("90011","不支持该类型订单付款");
                break;
        }

    }

    /**
     * 微信支付回调业务流程
     * */
    public function wechat_notify($order_id,$success){
        //已付订单
        $payed_order = Order::find($order_id);
        //订单用户
        $user = UserCheck::find($payed_order->user_id);
        //订单商户
        $shop = Shop::find($payed_order->shop_id);
        switch ($payed_order->order_type)
        {
            case 0://外卖
                if($payed_order->order_status==1){
                    $payed_order->order_status = 2;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    if($user){
                        $money = new MoneyChange();
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(外卖)订单编号：'.$payed_order->order_number,1,'外卖');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                     Log::info("notify error,order_id:".$payed_order->id);
                }
                break;
            case 1://预定
                if($payed_order->order_status==1){
                    $payed_order->order_status = 2;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    //加钱加流水
                    //$this->add_money($shop['user_id'],$payed_order->order_amount,1,'(预定)订单编号：'.$payed_order->order_number,1);
                    if($user){
                        //仅仅加流水
                        $money = new MoneyChange();
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(预定)订单编号：'.$payed_order->order_number,1,'预定');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else if($payed_order->order_status==3){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    //加钱加流水
                    $money = new MoneyChange();
                    $money->add_money($shop->user_id,$payed_order->order_price,1,'(结账)订单编号：'.$payed_order->order_number,1,'预定',$payed_order->id);
                    if($user){
                        //仅仅加流水
                        $money->add_wallet_log($user->id,$payed_order->order_price-$payed_order->order_amount,$user->money,0,'(结账)订单编号：'.$payed_order->order_number,1,'预定');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //数据库自增
                    $sqladd = new SqlIncrement();
                    $sqladd->dishIncrement($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);

                }else{
                    Log::info("notify error,order_id；".$payed_order->id);
                }
                break;
            case 2://排队
                if($payed_order->order_status==1){
                    $payed_order->order_status =4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    if($user){
                        //加流水
                        $money = new MoneyChange();
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(排队)订单编号：'.$payed_order->order_number,1,'排队');
                    }
                    //打印订单
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("notify error,order_id；".$payed_order->id);
                }
                break;
            case 3://扫码
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    //加钱加流水
                    $money = new MoneyChange();
                    $money->add_money($shop->user_id,$payed_order->order_price,1,'(扫码)订单编号：'.$payed_order->order_number,1,'扫码');
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(扫码)订单编号：'.$payed_order->order_number,1,'扫码');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //数据库自增
                    $sqladd = new SqlIncrement();
                    $sqladd->dishIncrement($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            case 4:
                /*if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    if($user){
                        //加流水
                        $this->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(扫码)订单编号：'.$payed_order->order_number,1);
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($this->order->id);
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }*/
                break;
            case 5:
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(充值)订单编号：'.$payed_order->order_number,1,'充值');
                    }
                    //加钱加流水
                    $money->add_money($payed_order->user_id,$payed_order->order_price,1,'(充值)订单编号：'.$payed_order->order_number,1,'充值');
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            case 6:
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(分享卡充值)订单编号：'.$payed_order->order_number,1,'分享卡');
                    }
                    //加钱加流水
                    $money->add_money($payed_order->user_id,$payed_order->order_price,1,'(分享卡充值)订单编号：'.$payed_order->order_number,1,'分享卡');
                    //获取分享人
                    $whoshare = OrderShare::where('order_id',$order_id)->first();
                    if($whoshare){
                        $sharecare = config('Text')['shareCards'];
                        $commission = false;
                        foreach($sharecare as $v){
                            if($v['money']==$payed_order->order_amount){
                                $commission = $v['commission'];
                            }
                        }
                        if($commission){
                            $money->add_money($whoshare->whoshare_id,$commission,1,'(分享佣金)订单编号：'.$payed_order->order_number,1,'分享佣金');
                        }
                    }
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            default://错误
                Log::info("wechat notify error,unknow order in where?");
                break;
        }
        return  $success;
    }

    /**
     *支付宝支付
     */
    protected function alipay(){
        switch ($this->order->order_type)
        {
            case 0://外卖
            case 2://排队
            case 3://扫码
            case 4://直接付款
            case 5://充值
                if($this->order->order_status!=1){
                    return show("90010","该订单状态不可支付！");
                }
                //订单价格处理
                $order = [
                    'out_trade_no' => $this->order->order_number.'_'.$this->order->id,
                    'subject' => '掌里通订单支付',
                    'total_fee' => $this->order->order_amount,
                ];
                break;
            case 6://优惠卡
                if($this->order->order_status!=1){
                    return show("90010","该订单状态不可支付！");
                }
                $money = false;
                $sharecard = config('Text')['shareCards'];
                foreach($sharecard as $v){
                    if($v['money']==$this->order->order_amount){
                        $money = true;
                    }
                }
                if(!$money){
                    return show(90010,'该订单不可支付');
                }
                //订单价格处理
                $order = [
                    'out_trade_no' => $this->order->order_number.'_'.$this->order->id,
                    'subject' => '掌里通订单支付',
                    'total_fee' => $this->order->order_amount,
                ];
                break;
            case 1://预定
                if($this->order->order_status==1){
                    //订单价格处理
                    $order = [
                        'out_trade_no' => $this->order->order_number.'-1'.'_'.$this->order->id,
                        'subject' => '掌里通订单支付',
                        'total_fee' => $this->order->order_amount,
                    ];
                }else if($this->order->order_status==3){
                    //订单价格处理
                    $order = [
                        'out_trade_no' => $this->order->order_number.'-2'.'_'.$this->order->id,
                        'subject' => '掌里通订单支付',
                        'total_fee' => $this->order->order_price-$this->order->order_amount,
                    ];
                }else{
                    return show("90010","该订单状态不可支付！");
                }
                break;
            default://错误
                return show("90011","不支持该类型订单付款");
                break;
        }
        return Pay::alipay($this->config['alipay'])->wap($order);
    }

    /**
     * 支付宝支付回调业务流程
     * */
    public function alipay_notify($order_id,$success){
        //已付订单
        $payed_order = Order::find($order_id);
        //订单用户
        $user = UserCheck::find($payed_order->user_id);
        //订单商户
        $shop = Shop::find($payed_order->shop_id);
        switch ($payed_order->order_type)
        {
            case 0://外卖
                if($payed_order->order_status==1){
                    $payed_order->order_status = 2;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(外卖)订单编号：'.$payed_order->order_number,2,'外卖');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("notify error,order_id:".$order_id);
                }
                break;
            case 1://预定
                if($payed_order->order_status==1){
                    $payed_order->order_status = 2;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    //加钱加流水
                    //$this->add_money($shop['user_id'],$payed_order->order_amount,1,'(预定)订单编号：'.$payed_order->order_number,1);
                    $money = new MoneyChange();
                    if($user){
                        //仅仅加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(预定)订单编号：'.$payed_order->order_number,2,'预定');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //数据库自增
                    $sqladd = new SqlIncrement();
                    $sqladd->dishIncrement($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);

                }else if($payed_order->order_status==3){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    //加钱加流水
                    $money = new MoneyChange();
                    $money->add_money($shop->user_id,$payed_order->order_price,1,'(结账)订单编号：'.$payed_order->order_number,2,'预定',$payed_order->id);
                    if($user){
                        //仅仅加流水
                        $money->add_wallet_log($user->id,$payed_order->order_price-$payed_order->order_amount,$user->money,0,'(结账)订单编号：'.$payed_order->order_number,2,'预定');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //数据库自增
                    $sqladd = new SqlIncrement();
                    $sqladd->dishIncrement($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("notify error,order_id；".$order_id);
                }
                //打印订单
                //推送消息
                break;
            case 2://排队
                if($payed_order->order_status==1){
                    $payed_order->order_status =4;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(排队)订单编号：'.$payed_order->order_number,2,'排队');
                    }
                    //打印订单
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("notify error,order_id；".$order_id);
                }
                break;
            case 3://扫码
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    //加钱加流水
                    $money = new MoneyChange();
                    $money->add_money($shop->user_id,$payed_order->order_price,1,'(扫码)订单编号：'.$payed_order->order_number,2,'扫码');
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(扫码)订单编号：'.$payed_order->order_number,2,'扫码');
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($order_id);
                    //推送消息
                    $wxsend = new WechatSend();
                    $wxsend->orderTemplate($order_id);
                    //数据库自增
                    $sqladd = new SqlIncrement();
                    $sqladd->dishIncrement($order_id);
                    //app语音
                    $appsend = new Jpush();
                    $appsend->sendOrder($order_id);
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            case 4:
                /*if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 1;
                    $payed_order->save();
                    if($user){
                        //加流水
                        $this->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(扫码)订单编号：'.$payed_order->order_number,1);
                    }
                    //打印订单
                    $printer = new CouldPrint();
                    $printer->printer($this->order->id);
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }*/
                break;
            case 5:
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(充值)订单编号：'.$payed_order->order_number,2,'充值');
                    }
                    //加钱加流水
                    $money->add_money($payed_order->user_id,$payed_order->order_price,1,'(充值)订单编号：'.$payed_order->order_number,2,'充值');
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            case 6:
                if($payed_order->order_status==1){
                    $payed_order->order_status = 4;
                    $payed_order->pay_way = 2;
                    $payed_order->save();
                    $money = new MoneyChange();
                    if($user){
                        //加流水
                        $money->add_wallet_log($user->id,$payed_order->order_amount,$user->money,0,'(分享卡充值)订单编号：'.$payed_order->order_number,2,'分享卡');
                    }
                    //加钱加流水
                    $money->add_money($payed_order->user_id,$payed_order->order_price,1,'(分享卡充值)订单编号：'.$payed_order->order_number,2,'分享卡');
                    //获取分享人
                    $whoshare = OrderShare::where('order_id',$order_id)->first();
                    if($whoshare){
                        $sharecare = config('Text')['shareCards'];
                        $commission = false;
                        foreach($sharecare as $v){
                            if($v['money']==$payed_order->order_amount){
                                $commission = $v['commission'];
                            }
                        }
                        if($commission){
                            $money->add_money($whoshare->whoshare_id,$commission,1,'(分享佣金)订单编号：'.$payed_order->order_number,1,'分享佣金');
                        }
                    }
                    //推送消息
                }else{
                    Log::info("wechat notify error,order_id；".$payed_order->id);
                }
                break;
            default://错误
                Log::info("notify error,unknow order in where?");
                break;
        }
        return  $success;
    }


}
